package com.cyou.qb.common.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.builder.ToStringBuilder;

import com.cyou.qb.constants.Constants;

public class JiuGongGeEx implements Serializable,Comparable<JiuGongGeEx>,Cloneable{
	
	private static final long serialVersionUID = 623271842772003296L;

	private int position; //位置
	
	private int activeId; 
	
	private int awardId;
	
	private String awardName;	
	
	private String awardImage; //图片链接
	
	private String tips;
	
	private static JiuGongGeEx default1 = new JiuGongGeEx(-1, -1, -1, Constants.defaultAward1Name, Constants.defaultAward1Image);
	private static JiuGongGeEx default2 = new JiuGongGeEx(-1, -1, -1, Constants.defaultAward2Name, Constants.defaultAward2Image);
	private static JiuGongGeEx default3 = new JiuGongGeEx(-1, -1, -1, Constants.defaultAward3Name, Constants.defaultAward3Image);
	private static JiuGongGeEx default4 = new JiuGongGeEx(-1, -1, -1, Constants.defaultAward4Name, Constants.defaultAward4Image);
	
	public JiuGongGeEx() {
		super();
	}

	public JiuGongGeEx(int position, int activeId, int awardId, String awardName, String awardImage) {
		super();
		this.position = position;
		this.activeId = activeId;
		this.awardId = awardId;
		this.awardName = awardName;
		this.awardImage = awardImage;
	}

	public static List<JiuGongGeEx> fillEmptyPosition(List<JiuGongGeEx> list,int activeId){
		if(list==null||list.size()==0||list.size()>=8){
			throw new RuntimeException("参数不正确");
		}
		List<JiuGongGeEx> ret = new ArrayList<JiuGongGeEx>(8);
		Collections.sort(list);
		Map<String,JiuGongGeEx>map = new HashMap<String, JiuGongGeEx>(list.size());
		for (JiuGongGeEx jiuGongGeEx : list) {
			map.put(jiuGongGeEx.getPosition()+"", jiuGongGeEx);
		}
		for (int i = 0; i < 8; i++) {
			if(map.get(i+"")!=null){
				ret.add(map.get(i+""));
			}else{
				try {
					ret.add(getDefault(i,activeId));
				} catch (CloneNotSupportedException e) {
					e.printStackTrace();
				}
			}
		}
		return ret;
	}
	
	
	private static JiuGongGeEx getDefault(int i,int activeId) throws CloneNotSupportedException {
		JiuGongGeEx ret= null;
		switch((i+activeId)%8){
			case 0:
				ret = (JiuGongGeEx) default1.clone();
				break;
			case 1:
				ret = (JiuGongGeEx) default2.clone();
				break;
			case 2:
				ret = (JiuGongGeEx) default3.clone();
				break;
			case 3:
				ret = (JiuGongGeEx) default4.clone();;
				break;
			case 4:
				ret = (JiuGongGeEx) default1.clone();
				break;
			case 5:
				ret = (JiuGongGeEx) default2.clone();;
				break;
			case 6:
				ret = (JiuGongGeEx) default3.clone();
				break;
			case 7:
				ret = (JiuGongGeEx) default4.clone();;
				break;
		}
		ret.setPosition(i);
		return ret;
	}


	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}

	public int getActiveId() {
		return activeId;
	}

	public void setActiveId(int activeId) {
		this.activeId = activeId;
	}

	public int getAwardId() {
		return awardId;
	}

	public void setAwardId(int awardId) {
		this.awardId = awardId;
	}

	public String getAwardName() {
		return awardName;
	}

	public void setAwardName(String awardName) {
		this.awardName = awardName;
	}

	public String getAwardImage() {
		return awardImage;
	}

	public void setAwardImage(String awardImage) {
		this.awardImage = awardImage;
	}
	public String toString(){
		return ToStringBuilder.reflectionToString(this);
	}
	public String getTips() {
		return tips;
	}

	public void setTips(String tips) {
		this.tips = tips;
	}
	@Override
	public int compareTo(JiuGongGeEx o) {
		if(o==null)
			return -1;
		if(o.getPosition()>this.position)
			return -1;
		if(o.getPosition()<this.position)
			return 1;
		if(o.position==this.position)
			return 0;
		return 0;
	}
	
	


	public static void main(String[] args) {
		List<JiuGongGeEx> list =new ArrayList<JiuGongGeEx>();
		
		JiuGongGeEx ex1 = new JiuGongGeEx();
		ex1.setPosition(100);
		list.add(ex1);
		
		JiuGongGeEx ex2 = new JiuGongGeEx();
		ex2.setPosition(50);
		list.add(ex2);
		
		JiuGongGeEx ex3 = new JiuGongGeEx();
		ex3.setPosition(2000);
		list.add(ex3);
		
		Collections.sort(list);
		System.out.println(list);
	}
}

